package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.service.OrderSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.*;

@DubboService(timeout = 10000)
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public boolean oneDaySetting(String day, Integer num) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",day);
        OrderSetting orderSetting = this.getOne(queryWrapper);
        if (orderSetting==null) {
            orderSetting=new OrderSetting();
        }
        Date date = DateUtil.parseDate(day);
        orderSetting.setOrderDate(date);
        orderSetting.setNumber(num);
        return this.saveOrUpdate(orderSetting);
    }

    @Override
    public void updateReservations(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        OrderSetting orderSetting = this.getOne(queryWrapper);
        boolean b = false;
        try {
            b = orderSetting.getReservations() >= orderSetting.getNumber();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("请选取体检日期");
        }
        if (b){
            throw new RuntimeException("该日预约已满");
        }
        baseMapper.reduceReservations(orderDate);
    }

    @Override
    public void importOrderSetting(List<String[]> list) {
        List<OrderSetting> settings = new ArrayList<>();
        if (list!=null){
            for (String[] rows : list) {

                QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("order_date",rows[0]);
                OrderSetting orderSetting = this.getOne(queryWrapper);

                if (orderSetting==null) {
                    orderSetting=new OrderSetting();
                }
                Date date = DateUtil.parseDate(rows[0]);
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(Integer.parseInt(rows[1]));
                settings.add(orderSetting);
            }
            this.saveOrUpdateBatch(settings);
        }
    }

    @Override
    public Map<String, OrderSetting> findByYearMonth(Integer year, Integer month) {
        String s = year+"-"+ (month<10?"0"+month:month);
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("order_date",s);
        List<OrderSetting> list = this.list(queryWrapper);
        Map<String, OrderSetting> map = new HashMap<>();
        for (OrderSetting orderSetting : list) {
            map.put(DateUtil.formatDate(orderSetting.getOrderDate()),orderSetting);
        }
        return map;
    }


    @Override
    public void cleanTable(String formatDate) {
        baseMapper.cleanTable(formatDate);
    }
}